package cn.tp.ksoft.civil_driver.module.order;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.OrderBean;
import cn.tp.ksoft.civil_driver.bean.OrderDetailBean;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.DriverOrderRequest;
import cn.tp.ksoft.civil_driver.utils.Constants;
import cn.tp.ksoft.civil_driver.utils.Utils;
import cn.tp.ksoft.library_civil.utils.DateUtil;

/**
 * Created by zhusong on 16/5/2.
 */
public class FinishOrderDetailActivity extends DriverBaseActivity implements View.OnClickListener{
    private final int REQUEST_CONFIRM_DURATION = 1;
    private OrderBean orderBean;

    private TextView mAddressTv;
    private TextView mUserTv;
    private TextView mStatusTv;
    private TextView mOrderNoTv;
    private TextView mTimeTv;
    private TextView mStartTimeTv;
    private TextView mDurationTv;
    private TextView mPriceTv;

    private Button mFinishBtn;

    private boolean isRecCustom = false;
    private DriverOrderRequest request;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_order_detail_finish);
        request = new DriverOrderRequest(this, new WeakHandler(this));
        updateTitle(getString(R.string.driver_finish_service));
        findViewById(R.id.btn_phone).setOnClickListener(this);
        findViewById(R.id.btn_finish).setOnClickListener(this);
        mAddressTv = (TextView) findViewById(R.id.tv_address);
        mUserTv = (TextView) findViewById(R.id.tv_user);
        mStatusTv = (TextView) findViewById(R.id.tv_status);
        mOrderNoTv = (TextView) findViewById(R.id.tv_orderNo);
        mTimeTv = (TextView) findViewById(R.id.tv_time);
        mStartTimeTv = (TextView) findViewById(R.id.tv_start_time);
        mDurationTv = (TextView) findViewById(R.id.tv_duration);
        mPriceTv = (TextView) findViewById(R.id.tv_price);
        mFinishBtn = (Button) findViewById(R.id.btn_finish);

        Intent intent = getIntent();
        if (null != intent) {
            orderBean = (OrderBean) intent.getSerializableExtra(Constants.KEY_ORDER_BEAN);
            OrderDetailBean detailBean = orderBean.getDetailBean();
            mAddressTv.setText(detailBean.getStartAddress());
            mUserTv.setText(orderBean.getName() +"    "+orderBean.getTel());
            mStatusTv.setText(getStatus(orderBean.getStatus()));
            mOrderNoTv.setText("订单号:"+orderBean.getOrderNo());
            mTimeTv.setText(DateUtil.D2Syrsf(orderBean.getOrderTime()));
            mStartTimeTv.setText(DateUtil.D2Syrsf(detailBean.getBycarStartTime()));
            mDurationTv.setText(DateUtil.hours2day(detailBean.getBycarLong()));
            mPriceTv.setText(orderBean.getOrderMoney()+"");
            isRecCustom = orderBean.getValidTime() != null;
            updateOrderStatus();
        }
    }

    private void updateOrderStatus() {
        if (isRecCustom) {
            mFinishBtn.setText(getString(R.string.driver_order_btn_finish));
        } else {
            mFinishBtn.setText(getString(R.string.driver_finish_get_customer));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CONFIRM_DURATION) {
            if (resultCode == RESULT_OK) {
                setResult(RESULT_OK);
                finish();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_finish : {
                if (!isRecCustom) {
                    request.recCustom(orderBean.getId());
                } else {
                    Intent intent = new Intent(this, OrderConfirmActivity.class);
                    intent.putExtra(Constants.KEY_ORDER_BEAN, orderBean);
                    startActivityForResult(intent, REQUEST_CONFIRM_DURATION);
                }
                break;
            }
            case R.id.btn_phone : {
                if (!Utils.isPhone(orderBean.getTel())) {
                    Toast.makeText(this, "未识别的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                //用intent启动拨打电话
                try {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+orderBean.getTel()));
                    startActivity(intent);
                } catch (SecurityException e) {
                    Toast.makeText(this, "拨号功能被禁用", Toast.LENGTH_SHORT).show();
                }

                break;
            }
        }
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        if (what == REQUEST_ORDER_START) {
            isRecCustom = true;
            updateOrderStatus();
            setResult(RESULT_OK);
        }
    }
}
